<?php namespace App\Http\Controllers\Index;

/**
 * 用户账号管理操作相关
 */
use App\Http\Controllers\Controller;

use App\Models\Customer;
use App\Models\Customer_day;
use App\Models\Customer_month;
use App\Models\Employee;
use App\Models\Evaluation;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;

class indexController extends Controller {

    public  function index(){

        /*$limit=8;

        $employees=Employee::where([])->orderBy('created_at','desc')->take($limit)->get();;

        $employee_json=json_encode($employees);*/

        $num=ceil(Employee::all()->count());

        return view('index/index')->with('num',$num);
        //print_r($employee_json);
    }

    public function getEmployees(){

        //$limit=9;

        //$employees=Employee::where([])->orderBy('created_at','desc')->take($limit)->get();

        $employees=DB::table('employee')->simplePaginate(8);

        $num=Employee::all()->count();

        $employee_json=json_encode($employees);

        echo $employee_json;
        //print_r(ceil($num/9));
    }

    public function getAllNum(){
        $num=Employee::all()->count();

        echo $num;
    }

    public function getData(){
        $name=$_POST['name'];
        $phone=$_POST['phone'];
        $pick=$_POST['pick'];

        $data=json_decode($pick);



        /*$customer=new Customer;
        $customer->name=$name;
        $customer->phoneNumber=$phone;

        $customer->save();*/
        $customer=Customer::where(['name'=>$name,'phoneNumber'=>$phone,'check_in'=>'入住'])->first();
        if($customer!=null){
            if($customer->evaluation_type==0){
                $id=$customer->id;
                $customer->evaluation_type=1;
                $customer->save();

                foreach($data as $key=>$value)
                {
                    $Evaluation=new Evaluation;
                    $Evaluation->customerId=$id;
                    $Evaluation->employeeId=$key;
                    $Evaluation->content=$value;

                    $Evaluation->save();
                }

                $day=date("Y-m-d");
                $customer_day=Customer_day::where(['day_num'=>$day])->first();
                if($customer_day!=null){
                    $customer_day->num=$customer_day->num+1;
                }else{
                    $customer_day=new Customer_day();
                    $customer_day->day_num=$day;
                    $customer_day->num=1;
                }
                $customer_day->save();



                $month=date("Y-m");
                $customer_month=Customer_month::where(['month_num'=>$month])->first();
                if($customer_month!=null){
                    $customer_month->num=$customer_month->num+1;
                }else{
                    $customer_month=new Customer_month();
                    $customer_month->month_num=$month;
                    $customer_month->num=1;
                }
                $customer_month->save();

                echo '评价成功';
            }else{
                echo '对不起，您已评价过';
            }

        }else{
            echo '对不起，您没有入住该酒店';
        }

        //$form_json=json_encode($form);
        //echo $pick;
    }



}